package com.topone.Converter;

import com.topone.Exception.FanZoneServiceException;
import com.topone.Form.CouponForm;
import com.topone.Utils.ServiceUtils;
import com.topone.dataObject.TcCoupon;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;

@Slf4j
public class TcCouponForm2TcCouponConvert {

    public static TcCoupon convert(CouponForm couponForm) {
        TcCoupon tcCoupon = new TcCoupon();
        BeanUtils.copyProperties(couponForm, tcCoupon);
        log.info("------couponForm-------{}", ServiceUtils.toJson(couponForm));
        try {
            tcCoupon.setCouponPrice(new BigDecimal(couponForm.getCouponPrice()));
            tcCoupon.setDiscountPrice(new BigDecimal(couponForm.getDiscountPrice()));
            if (StringUtils.isEmpty(couponForm.getUseDurationStart())) {
                tcCoupon.setUseDurationStart(ServiceUtils.getStandardFirstDate());
            } else {
                tcCoupon.setUseDurationStart(ServiceUtils.getCurrentDate(couponForm.getUseDurationStart()));
            }

            if (StringUtils.isEmpty(couponForm.getUseDurationEnd())) {
                tcCoupon.setUseDurationEnd(ServiceUtils.getStandardLastDate());
            } else {
                tcCoupon.setUseDurationEnd(ServiceUtils.getCurrentDate(couponForm.getUseDurationEnd()));
            }

            if (StringUtils.isEmpty(couponForm.getActivityDurationStart())) {
                tcCoupon.setActivityDurationStart(ServiceUtils.getStandardFirstDate());
            } else {
                tcCoupon.setActivityDurationStart(ServiceUtils.getCurrentDate(couponForm.getActivityDurationStart()));
            }

            if (StringUtils.isEmpty(couponForm.getActivityDurationEnd())) {
                tcCoupon.setActivityDurationEnd(ServiceUtils.getStandardLastDate());
            } else {
                tcCoupon.setActivityDurationEnd(ServiceUtils.getCurrentDate(couponForm.getActivityDurationEnd()));
            }

            if (StringUtils.isNotEmpty(couponForm.getUseDurationEndTime())) {
                tcCoupon.setUseDurationEndTime(ServiceUtils.getCurrentTime(couponForm.getUseDurationEndTime()));
            }

            if (StringUtils.isNotEmpty(couponForm.getUseDurationStartTime())) {
                tcCoupon.setUseDurationStartTime(ServiceUtils.getCurrentTime(couponForm.getUseDurationStartTime()));
            }

            if (StringUtils.isNotEmpty(couponForm.getLowestConsume())) {
                tcCoupon.setLowestConsume(new BigDecimal(couponForm.getLowestConsume()));
            }

            if (StringUtils.isNotEmpty(couponForm.getCouponValue())) {
                tcCoupon.setCouponValue(new BigDecimal(couponForm.getCouponValue()));
            }

            log.debug("-----------------tcCoupon---------------{}", ServiceUtils.toJson(tcCoupon));
        } catch (Exception e) {
            throw new FanZoneServiceException("couponForm数据转换失败:", e.fillInStackTrace());
        }
        return tcCoupon;
    }
}
